// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <SAXRequestsHandler.hpp>

SAXRequestsHandler::SAXRequestsHandler()
  : works(Work::ID_UNKNOWN)
{
}

void SAXRequestsHandler::startDocument ()
{
  _kattrSource = XMLString::transcode("source");
  _kattrTarget = XMLString::transcode("target");
  _kattrHops   = XMLString::transcode("hops");
}

void SAXRequestsHandler::endDocument ()
{
  XMLString::release(&_kattrSource);
  XMLString::release(&_kattrTarget);
  XMLString::release(&_kattrHops);
}

void SAXRequestsHandler::startElement ( const XMLCh* const uri,
		    const XMLCh* const localname,
		    const XMLCh* const qname,
		    const Attributes&  attrs )
{
  char * elem = XMLString::transcode(localname);

  if( XMLString::equals(elem, "distance") ) {
    request_type = R_DISTANCE;
    char * source_ch = XMLString::transcode( attrs.getValue( _kattrSource ) );
    char * target_ch = XMLString::transcode( attrs.getValue( _kattrTarget ) );
    source = source_ch;
    target = target_ch;
    XMLString::release(&source_ch);
    XMLString::release(&target_ch);
  } else if ( XMLString::equals(elem, "distance_n") ) {
    request_type = R_DISTANCEN;
    char * source_ch = XMLString::transcode( attrs.getValue( _kattrSource ) );
    char * hops_ch = XMLString::transcode( attrs.getValue( _kattrHops ) );
    source = source_ch;
    hops = (unsigned int)atoi(hops_ch);
    XMLString::release(&source_ch);
    XMLString::release(&hops_ch);
  } else if ( XMLString::equals(elem, "center") ) {
    request_type = R_CENTER;
    char * source_ch = XMLString::transcode( attrs.getValue( _kattrSource ) );
    source = source_ch;
    XMLString::release(&source_ch);
  } else if ( XMLString::equals(elem, "min") ) {
    filt_proc = F_MIN;
  } else if ( XMLString::equals(elem, "max") ) {
    filt_proc = F_MAX;
  } else if ( XMLString::equals(elem, "suspended") ) {
    filt_proc = F_SUSPENDED;
  } else if ( XMLString::equals(elem, "media") ) {
    filt_proc = F_MEDIA;
  } else if ( XMLString::equals(elem, "genre") ) {
    filt_proc = F_GENRE;
  } else if ( XMLString::equals(elem, "work") ) {
    filt_proc = F_WORK;
  }

  XMLString::release(&elem);
}
  
void SAXRequestsHandler::endElement( const XMLCh* const uri, 
		 const XMLCh* const localname,
		 const XMLCh* const qname )
{ 
  char * elem = XMLString::transcode(localname);

  if ( XMLString::equals(elem, "min") ) {
    min_year = xml_get_year( buffer.c_str() );
  } else if ( XMLString::equals(elem, "max") ) {
    max_year = xml_get_year( buffer.c_str() );
  } else if ( XMLString::equals(elem, "suspended") ) {
    suspended = xml_get_suspended( buffer.c_str() );
  } else if ( XMLString::equals(elem, "media") ) {
    medias = medias | xml_get_media( buffer.c_str() );
  } else if ( XMLString::equals(elem, "genre") ) {
    genres = genres | xml_get_genre( buffer.c_str() );
  } else if ( XMLString::equals(elem, "work") ) {
    works = Work(works | xml_get_work( buffer.c_str() ));
  }

  XMLString::release(&elem);
  filt_proc = F_NONE;
  buffer.clear();
}


void SAXRequestsHandler::characters( const XMLCh* const chars,
				     const XMLSize_t length )
{
  if ( filt_proc != F_NONE )
    buffer.append(chars);
}

void SAXRequestsHandler::fatalError( const SAXParseException& exception )
{
  throw exception;
}

void SAXRequestsHandler::error( const SAXParseException &exception )
{
  throw exception;
}
